package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * 角色管理控制层
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    //引用服务
    @Reference
    private RoleService roleService;

    @Autowired
    private JedisPool jedisPool;


    //角色管理  分页查询
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    //角色管理  模糊查询
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult query(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.query(queryPageBean.getQueryString());
        return pageResult;
    }

    //角色管理  新增角色
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Role role) {
        try {
            roleService.add(role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }


    //回显角色数据
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Result findById(Integer groupId) {
        try {
            Role role = roleService.findById(groupId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //编辑角色
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result edit(@RequestBody Role role) {
        try {
            roleService.edit(role);
            return new Result(true, MessageConstant.ADD_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_EDIT_FAIL);
        }
    }

    //删除角色
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    public Result deleteById(Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_EDIT_FAIL);
        }
    }

    //发送请求获取页面回显参数  权限
    @RequestMapping(value = "/QueryAuthority", method = RequestMethod.POST)
    public Result QueryAuthority() {
        try {
            List<Permission> tableData = roleService.QueryAuthority();
            return new Result(true, MessageConstant.QUERY_JURISDICTION_SUCCESS, tableData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_JURISDICTION_FAIL);
        }
    }

    //发送请求获取页面回显参数  菜单
    @RequestMapping(value = "/InquiryMenu", method = RequestMethod.POST)
    public Result InquiryMenu() {
        try {
            List<Menu> Data = roleService.InquiryMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, Data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //回显权限选择项数据
    @RequestMapping(value = "/jurisdictionIds", method = RequestMethod.GET)
    public Result jurisdictionIds(Integer jurisdictionId) {
        try {
            List<Integer> jurisdictionIds = roleService.jurisdictionIds(jurisdictionId);
            return new Result(true, MessageConstant.QUERY_PERMISSIONSELECTION_SUCCESS, jurisdictionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIONSELECTION_FAIL);
        }
    }

    //回显菜单选择项数据
    @RequestMapping(value = "/menuIds", method = RequestMethod.GET)
    public Result menuIds(Integer menuId) {
        try {
            List<Integer> menuIds = roleService.menuIds(menuId);
            return new Result(true, MessageConstant.QUERY_MENUSELECTION_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENUSELECTION_FAIL);
        }
    }

    //分配权限与菜单   权限
    @RequestMapping(value = "/jurisdictionId", method = RequestMethod.POST)
    public Result jurisdictionId(@RequestBody List<Integer> jurisdictionIds, Integer id) {
        try {
            roleService.jurisdictionId(jurisdictionIds, id);
            return new Result(true, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }

    //分配权限与菜单   菜单
    @RequestMapping(value = "/menuId", method = RequestMethod.POST)
    public Result menuId(@RequestBody List<Integer> menuIds, Integer id) {
        try {
            roleService.menuId(menuIds, id);
            return new Result(true, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }

    //校验是否有关联
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public Result check(Integer id) {
        try {
            boolean nb = roleService.check(id);
            return new Result(true, "可以删除", nb);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "校验失败");
        }
    }

    /**
     * 根据roleId查询所用的user的id
     */
    @RequestMapping(value = "/findUserIdsByRoleId", method = RequestMethod.GET)
    public List<Integer> findUserIdsByRoleId(Integer roleId) {
        try {
            List<Integer> userIds=roleService.findUserIdsByRoleId(roleId);
            return userIds;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 存入角色和用户的关系表
     */
    @RequestMapping(value = "/editUser",method = RequestMethod.POST)
    private Result editUser(Integer[] userIds, @RequestBody Map map){
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.select(3);
            jedis.flushDB();
            Integer rId = (Integer) map.get("rId");
            roleService.editUser(userIds,rId);
            return new Result(true,"关联用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"关联用户失败");
        }
    }
}
